package thorsten.wordlist.parser;
import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.helpers.DefaultHandler;
import java.io.*;
import java.util.Date;
import java.util.Locale;

import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.apache.xerces.parsers.SAXParser;


public class XmlExtractor extends DefaultHandler {
	private SingleFileOutputHandler outHandler;
	
	@Override
	public void startDocument() throws SAXException {
		try {
			this.outHandler=new SingleFileOutputHandler();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.startDocument();
	}

	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) {
//		System.out.println(localName);
//		try {
//			this.outHandler.add(qName);
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
	}	

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String s=new String(ch);
		s = s.substring(start, start + length);
		
		try {
			this.outHandler.add(s);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		super.characters(ch, start, length);
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		try {
			this.outHandler.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.endElement(uri, localName, qName);
	}

	@Override
	public void endDocument() throws SAXException {
		try {
			this.outHandler.finish();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.endDocument();
	}
	
	/**
	 * @param args
	 * @throws IOException 
	 * @throws SAXNotSupportedException 
	 * @throws SAXNotRecognizedException 
	 */
	public static void main(String[] args) throws IOException, SAXNotRecognizedException, SAXNotSupportedException {
		XmlExtractor parser = new XmlExtractor();
	    SAXParser sp = new SAXParser();
//	    sp.setContentHandler(parser); //SingleFile
//	    sp.setContentHandler(new MultiFileOutputHandler()); //MultiFile
	    sp.setContentHandler(new MultiFileOutputHandlerDW()); //MultiFile
//	    sp.setContentHandler(new ElementCounterOutputHandler()); //ElementCount
	    sp.setFeature("http://xml.org/sax/features/validation", false);
	    sp.setFeature("http://apache.org/xml/features/validation/schema", false);
	    sp.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
	    sp.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
	    
	    long start = new Date().getTime();
	  	    
	    try {
	    	
	    	
//	    	sp.parse("D:/home/projekte/wordlist/data/testfile1.xml");
	    	
//	    	sp.parse("D:/home/projekte/wordlist/data/testfile2.xml"); 
	    	 
//	    	sp.parse("testdata/testfile3.xml"); 
	    	sp.parse("testdata/testfile2.xml"); 
//	    	sp.parse("testdata/dewikibooks-20130114-pages-meta-history.xml"); 
	    	
//	    	sp.parse("testdata/cc.xml"); 
	    	} 
	    catch (Exception e) {e.printStackTrace();}
	    
	    long runningTime = new Date().getTime() - start;
		System.out.println("Laufzeit: " + runningTime);
		
		
		System.out.println("fertig");

		//test
		
	}

}
